home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / untested / tcpip / web / access.sx next >
Encoding:
Text File  |  1996-05-21  |  2.1 KB  |  95 lines  |  [TEXT/ttxt]

  1. --<<<
  2.  
  3. in module WebImplementation
  4.  
  5. -- Basic URL access mechanism.
  6. -- Different protocols register an access function
  7. -- which must return a hashtable of MIME info and
  8. -- a stream from which data can be read.
  9.  
  10.  
  11. class WebAccessManager ()
  12.  class variables
  13.   accessMethods : (new HashTable)
  14. end
  15.  
  16. class method registerAccessMethod self {class WebAccessManager} name accessMethod -> (
  17.     local n := getlowercase (name as string)
  18.     deleteKeyOne self.accessMethods n
  19.     self.accessMethods[n] := accessMethod
  20. )
  21.     
  22.  
  23. class method getam self {class WebAccessManager} protocol op -> (
  24.     local am := self.accessMethods[getlowercase(protocol)]
  25.     
  26.     if (isakindof am collection) then
  27.         am := am[op]
  28.     else if not (op == @get) do
  29.         am := empty
  30.         
  31.     if (am == empty) do
  32.         report (new generalexception) #(protocol, op)
  33.     return am
  34. )
  35.  
  36. class method geturl self {class WebAccessManager} aURL -> (
  37.     if (not (isaKindof aURL url)) do
  38.         aURL := new url string: aURL
  39.  
  40.     local am := getam self (aURL.scheme as string) @get
  41.     am aURL
  42. )
  43.  
  44. class method postURL self {class WebAccessManager} aURL data #rest args -> (
  45.     if (not (isaKindof aURL url)) do
  46.         aURL := new url string: aURL
  47.  
  48.     local am := getam self (aURL.scheme as string) @post
  49.     apply am aURL data args
  50. )
  51.  
  52.  
  53. class method geturltofile self {class WebAccessManager} url dirrep name -> (
  54.     local p := geturl self url
  55.     local s;
  56.     createFile dirrep name @binary
  57.     s := getstream dirrep name @writable
  58.     pipe s p[2] 
  59.     plug s
  60.     plug p[2]
  61.     p[1]
  62. )
  63.  
  64. global count := 0
  65.  
  66. function getTempFileName -> (
  67.     local filename := format string "file%*.sxt" count
  68.     count := count + 1
  69.     filename
  70. )
  71.  
  72. function getURLToTempFile url -> (\
  73.     local filename := getTempFileName()
  74.     #(geturltofile WebAccessManager url thetempdir filename, filename)
  75. )
  76.  
  77. global openContainers := new hashTable
  78.  
  79. function openContainerFromURL data #key dir: (theTempDir) file: -> (
  80.     local tc := openContainers[data.string]
  81.     if tc = empty then
  82.         openContainers[data.string] := open titlecontainer dir: dir \
  83.                                             path: (if file = unsupplied then (getURLToTempFile data)[2] else file)
  84.     else
  85.         tc
  86. )
  87.  
  88.  
  89. function startWebToolkit tc -> (
  90.     if not (isdefined tcpstream) do process (new loader) "loadable/web"
  91.      foreach tc load undefined
  92. )
  93.  
  94. -->>>
  95.